package com.taylor.BaiYueGu.controller;


import com.taylor.BaiYueGu.common.JwtUtils;
import com.taylor.BaiYueGu.entity.UcenterMember;
import com.taylor.BaiYueGu.service.UcenterMemberService;
import com.taylor.BaiYueGu.vo.LoginInfoVo;
import com.taylor.BaiYueGu.vo.LoginVo;
import com.taylor.BaiYueGu.vo.RegisterVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.xmlbeans.impl.jam.mutable.MPackage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author Taylor
 * @since 2021-01-31
 */
@Api(tags = "用户登录注册api-张太娈")
@RestController
@RequestMapping("/BaiYueGu/ucenter-member")
public class UcenterMemberController {

    @Resource
    private UcenterMemberService memberService;

    @ApiOperation(value = "会员登录")
    @PostMapping("login")
    public Map<String,Object> login(@RequestBody LoginVo loginVo) {
        String token = memberService.login(loginVo);
        Map<String,Object> map = new HashMap<>(2);
        map.put("resultCode",200);
        map.put("token",token);
        return map;
    }

    @ApiOperation(value = "会员注册")
    @PostMapping("register")
    public Map<String,Object> register(@RequestBody RegisterVo registerVo){
        memberService.register(registerVo);
        Map<String,Object> map = new HashMap<>(2);
        map.put("resultCode",200);
        return map;
    }

    @ApiOperation(value = "根据token获取登录信息")
    @GetMapping("auth/getLoginInfo")
    public Map<String,Object> getLoginInfo(HttpServletRequest request){
        try {
            String memberId = JwtUtils.getMemberIdByJwtToken(request);
            LoginInfoVo loginInfoVo = memberService.getLoginInfo(memberId);
            Map<String, Object> map = new HashMap<>(2);
            map.put("resultCode",200);
            map.put("item",loginInfoVo);
            return map;
        }catch (Exception e){
            e.printStackTrace();
            return new HashMap<>(2);
        }
    }
}

